*------------------------------PLEASE READ-------------------------------------*

/*

REPLICATION FILE FOR FIGURES 1and A2
AUTHOR: ZHENHUAN LEI & YISHUANG LI
DATE: 11/13/2023
SOFTWARE: STATA 17 SE OPERATED ON WINDOWS 11

STATA PACKAGE NEEDED: 
	ssc install binscatter, replace

*/

*-------------------------- Set up the environment ---------------------------*

use ../source/wgidataset.dta, clear

* Merge Polity2 score into the dataset
	rename code scode
	merge m:m scode year using ../source/p5v2018
	drop _merge

	encode scode, generate(code2)
	xtset code2 year

* We use the Polity2 score in 2018 to replace the missing values in 2019 and 2020, assuming that the Polity2 score would be stable two years after 2018     
	replace polity2 = L.polity2 if year == 2019 & polity2 == .
	replace polity2 = L.polity2 if year == 2020 & polity2 == .
	replace xconst = L.xconst if year == 2019 & xconst == .
	replace xconst = L.xconst if year == 2020 & xconst == .

* Generate groups for figure plotting	
	gen isChina = (scode=="CHN")
	gen is_China2 = 2 if isChina == 1
	replace is_China2 = 1 if polity2 < 0 & isChina == 0
	replace is_China2 = 0 if polity2 >= 0

*-------------------------- Plotting -------------------------------*

***Figure 1*** 

	binscatter rlr year if year > 1995, by(is_China2) line(connect) ///
		discrete ///
		xlabel(#14) ///
		xline(2002.3, lp(dash)) ///
		xline(2012.5, lp(dash)) ///
		legend(off) ///
		colors(maroon eltblue) msymbol(O T) ///
		text(60 1998.5 "Jiang Zemin", color(black)) ///
		text(60 2007.5 "Hu Jintao", color(black)) ///
		text(60 2016.5 "Xi Jinping", color(black)) ///
		text(55 2007.5 "Democracies (POLITY2 >= 0)", color(maroon)) ///
		text(45 2009 "China", color(gs10)) ///
		text(23.5 2011 "Other autocracies (POLITY2 < 0)", color(eltblue)) ///
		xtitle(Year) ///
		ytitle("Rule of Law, Percentile Rank (0-100)") ///
		note(Data sources: Worldwide Governance Indicators (2020) and Polity5 Project (2018)) ///
		scheme(cleanplots) 
	graph export ../result/Figure1.png, width(800) height(600) replace	

***Figure A2***

	binscatter rle year if year > 1995, by(is_China2) line(connect) ///
		discrete ///
		xlabel(#14) ///
		xline(2002.3, lp(dash)) ///
		xline(2012.5, lp(dash)) ///
		legend(off) ///
		colors(maroon eltblue) msymbol(O T) ///
		text(0.2 1998.5 "Jiang Zemin", color(black)) ///
		text(0.2 2007.5 "Hu Jintao", color(black)) ///
		text(0.2 2016.5 "Xi Jinping", color(black)) ///
		text(0.14 2007.5 "Democracies (POLITY2 >= 0)", color(maroon)) ///
		text(-0.35 2009 "China", color(gs10)) ///
		text(-0.64 2015 "Other autocracies (POLITY2 < 0)", color(eltblue)) ///
		xtitle(Year) ///
		ytitle("WGI Rule of Law Score") ///
		note(Data sources: Worldwide Governance Indicators (2020) and Polity5 Project (2018)) ///
		scheme(cleanplots) 
	graph export ../result/FigureA2.png, width(800) height(600) replace	

